package com.chen.judge.strategy.StrategyImpl;

import com.chen.common.enums.JudgeInfoMessageEnum;
import com.chen.judge.codesandbox.model.JudgeInfo;
import com.chen.judge.codesandbox.model.JudgeResult;
import com.chen.judge.codesandbox.model.JudgeContext;
import com.chen.judge.strategy.JudgeStrategy;
import com.chen.popj.entity.JudgeConfig;
import com.chen.popj.entity.Problem;

import java.util.List;

/**
 * Java 程序的判题策略
 */
public class CPPLanguageJudgeStrategy implements JudgeStrategy {

    /**
     * 执行判题
     *
     * @param judgeContext
     * @return
     */
    @Override
    public JudgeResult doJudge(JudgeContext judgeContext) {
        System.out.println("C/CPP-使用测试点进行判题");
        List<JudgeInfo> judgeInfoList = judgeContext.getJudgeInfoList();
//        System.out.println(judgeInfoList);
//        Long memory = judgeInfo.getMemory();
//        Long time = judgeInfo.getTime();
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        Problem problem = judgeContext.getProblem();
//        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;
        JudgeResult judgeResultResponse = new JudgeResult();
        judgeResultResponse.setMessage(judgeInfoMessageEnum.getValue());
        // judgeResultResponse.setMemory(memory);
        // judgeResultResponse.setTime(time);
        // 先判断沙箱执行的结果输出数量是否和预期输出数量相等
        if (outputList.size() != inputList.size()) {
            judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeResultResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeResultResponse;
        }
        // 依次判断每一项输出和预期输出是否相等
        for (int i = 0; i < judgeInfoList.size(); i++) {
            JudgeInfo judgeCase = judgeInfoList.get(i);
            if (!judgeCase.getMessage().equals(outputList.get(i))) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeResultResponse.setMessage(judgeInfoMessageEnum.getValue());
                judgeInfoList.get(i).setMessage(judgeInfoMessageEnum.getValue());
//                return judgeResultResponse;
            } else {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;
                judgeInfoList.get(i).setMessage(judgeInfoMessageEnum.getValue());
            }
        }
        // 判断题目限制
        // String judgeConfigStr = problem.getJudgeConfig();
        // JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        JudgeConfig judgeConfig = JudgeConfig.builder()
                .timeLimit(problem.getTimeLimit())
                .memoryLimit(problem.getMemoryLimit())
                .build();
        Integer needMemoryLimit = judgeConfig.getMemoryLimit();
        Integer needTimeLimit = judgeConfig.getTimeLimit();
        for (JudgeInfo judgeInfo : judgeInfoList) {
            if (judgeInfo.getMemory() > needMemoryLimit) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
                judgeResultResponse.setMessage(judgeInfoMessageEnum.getValue());
            }
            if (judgeInfo.getTime() > needTimeLimit) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
                judgeResultResponse.setMessage(judgeInfoMessageEnum.getValue());
            }
//            judgeResultResponse.setMessage(judgeInfoMessageEnum.getValue());
        }
        judgeResultResponse.setJudgeInfoList(judgeInfoList);
        System.out.println("judgeResultResponse");
        System.out.println(judgeResultResponse);
        return judgeResultResponse;
    }
}

